<?php

/**
 *  class Pagination
 *
 *  独立分页类
 *  调用方式：
 *  $pagenation = new Pagination( 4, 10, 200 ); // 4(第一个参数) = currentPage, 10(第二个参数) = pageSize, 200(第三个参数) = 总数
 *  $pagenation->set_link( 'http://www.it308.com' );
 *  $pagenation->show();
 */
class Pagination {

    protected $_total = 0;
    protected $_total_page = 0;
    protected $_page = 1;
    protected $_page_size = 10;
    protected $_link = '';
    protected $_grep = 3;
    protected $_admin = false;
    protected $_css_next = 'next-page';
    protected $_css_prev = 'prev-page';
    protected $_css_curr = 'curr-page';
    protected $_css_page = 'page-nav inline-block';

    //修改为CI使用的数组方式  $currentpage, $page_size, $total
    public function __construct($config = array()) {
        if (count($config) > 0) {
            foreach ($config as $k => $v) {
                $this->$k = $v;
            }
        }
        $this->set_current_page($this->page);
        $this->set_page_size($this->page_size);
        $this->set_total($this->total);
        $this->_admin = true;
        //$this->_link = $_SERVER['REQUEST_URI'];
        //处理字符串末尾的数字
        $uri = $_SERVER['REQUEST_URI'];
        $uri = rtrim($uri, '/');
        $pos = strrpos($uri, '/');
        if ($pos > 0) {
            //判断最后的是否是数字
            $str = substr($uri, $pos, strlen($uri));
            //$str = ltrim($str,'/');
            if (is_numeric($str)) {
                //最后一个参数是分页数据
                $uri = substr($uri, 0, $pos);
            } else {
                //最后一个参数不是分页数据
                $uri = $_SERVER['REQUEST_URI'];
            }
        }
        $uri = rtrim($uri, '/');

        $this->set_link($uri);
    }

    public function set_link($link, $is_shop = TRUE) {
        $len = strlen($link);
        $substr = substr($link, $len - 1);
        if ('&' == $substr) {
            $link = substr($link, 0, $len - 1);
        }
        $pos = strpos($link, '?');
        if ($pos) {
            $link = substr($link, 0, $pos);
        }
        if (!empty($_GET)) {
            $link .= '?';
            foreach ($_GET as $k => $v) {
                if ('page' == strtolower($k)) {
                    continue;
                }
                $link .= $k . '=' . $v . '&';
            }
            $len = strlen($link);
            $substr = substr($link, $len - 1);
            if ('&' == $substr) {
                $link = substr($link, 0, $len - 1);
            }
        } elseif (isset($_SERVER['QUERY_STRING']) AND ! empty($_SERVER['QUERY_STRING']) AND $is_shop) {
            $link .= '?' . $_SERVER['QUERY_STRING'];
            $len = strlen($link);
            $substr = substr($link, $len - 1);
            if ('&' == $substr) {
                $link = substr($link, 0, $len - 1);
            }
        }
        $this->_link = $link;
    }

    public function set_page_size($page_size) {
        if (empty($page_size)) {
            $this->_page_size = 10;
        } else {
            $this->_page_size = (int) $page_size;
        }
    }

    public function set_total($total) {
        $page_size = empty($this->_page_size) ? 10 : $this->_page_size;
        $this->_total = $total;
        if (0 == ( $total % $page_size )) {
            $this->_total_page = intval($total / $page_size);
        } else {
            $this->_total_page = intval($total / $page_size) + 1;
        }
        if ($this->_page > $this->_total_page) {
            $this->_page = $this->_total_page;
        }
    }

    public function set_current_page($page) {
        if (empty($page)) {
            $this->_page = 1;
        } else {
            $this->_page = (int) $page;
        }
    }

    public function get_next_page_btn() {
        if ($this->_page < $this->_total_page) {
            $link = '';

            if ($tmp = strpos($this->_link, '?')) {
                $link = $this->_link . '&page=' . ( $this->_page + 1 );
            } else {
                $link = $this->_link . '?page=' . ( $this->_page + 1 );
            }
            if ($this->_admin) {
                return '<a href="' . $link . '"> 下一页</a>';
            } else {
                return '<a href="' . $link . '"> 下一页</a>';
            }
        }
        if ($this->_admin)
            return ' 下一页';
        else
            return '';
    }

    public function get_prev_page_btn() {
        if ($this->_page > 1) {
            $link = '';
            if (strpos($this->_link, '?')) {
                $link = $this->_link . '&page=' . ( $this->_page - 1 );
            } else {
                $link = $this->_link . '?page=' . ( $this->_page - 1 );
            }
            if ($this->_admin) {
                return '<a href="' . $link . '">上一页</a>';
            } else {
                return '<a href="' . $link . '">上一页</a>';
            }
        }
        if ($this->_admin)
            return '上一页';
        else
            return '';
    }

    public function get_current_page() {
        if ($this->_admin)
            return '<strong>' . $this->_page . '</strong>';
        else
            return '<span class="current">' . $this->_page . '</span>';
    }

    public function get_page_link($page) {
        $link = '';
        if (strpos($this->_link, '?')) {
            $link = $this->_link . '&page=' . $page;
        } else {
            $link = $this->_link . '?page=' . $page;
        }
        if ($this->_admin) {
            return '<a href="' . $link . '">' . $page . '</a>';
        } else {
            return '<a href="' . $link . '">' . $page . '</a>';
        }
    }

    public function get_prev_pages() {
        $pages = array();
        $begin = $this->_page - $this->_grep;
        if ($begin < 1) {
            $begin = 1;
        } elseif ($begin > 2) {
            $pages[] = $this->get_page_link(1);
            if ($this->_admin) {
                $pages[] = '......';
            } else {
                $pages[] = '......';
            }
        } elseif ($begin == 2) {
            $pages[] = $this->get_page_link(1);
        }
        for ($i = $begin; $i < $this->_page; $i++) {
            $pages[] = $this->get_page_link($i);
        }
        return $pages;
    }

    public function get_next_pages() {
        $pages = array();
        $begin = $this->_page + 1;
        if ($begin < $this->_total_page) {
            $end = $begin + $this->_grep;
            if ($end > $this->_total_page) {
                $end = $this->_total_page;
            }
            for ($i = $begin; $i < $end; $i++) {
                $pages[] = $this->get_page_link($i);
            }
            if ($i < $this->_total_page) {
                if ($this->_admin) {
                    $pages[] = '&nbsp;...&nbsp;';
                } else {
                    $pages[] = '......';
                }
                $pages[] = $this->get_page_link($this->_total_page);
            } else {
                $pages[] = $this->get_page_link($this->_total_page);
            }
        } elseif ($begin == $this->_total_page) {
            $pages[] = $this->get_page_link($this->_total_page);
        }
        return $pages;
    }

    public function jump_page() {

        if ($tmp = strpos($this->_link, '?')) {
            $link = $this->_link . '&page=';
        } else {
            $link = $this->_link . '?page=';
        }
        echo " 跳到";
        echo "<select onchange='javascript:location.href=\"" . $link . "\"+this.options[this.selectedIndex].value'>";
        for ($i = 1; $i <= $this->_total_page; $i++) {
            if ($this->_page == $i) {
                echo "<option selected='selected' value='" . $i . "'>" . $i . "</option>";
            } else {
                echo "<option value='" . $i . "'>" . $i . "</option>";
            }
        }
        echo "</select>";
    }

    public function show() {

        if ($this->_total_page <= 1) {
            //return;
        }
        echo "<div id='pager'>";
        echo "共<b>" . $this->_total_page . "</b>页";
        echo ' 第<b>' . $this->_page . '</b>页 <b>' . $this->_page_size . '</b>条/页';
        echo ' 共<b>' . $this->_total . '</b>条记录 ';
        if ($this->_page != 1) {
            echo " <a href=" . $this->_link . ">首页</a> ";
        }
        echo $this->get_prev_page_btn();
        echo $this->get_next_page_btn();
        $cntNum = ceil($this->_total / $this->_page_size);
        if (strpos($this->_link, '?')) {
            $link = $this->_link . "&page={$cntNum}";
        } else {
            $link = $this->_link . "?page={$cntNum}";
        }
        if ($this->_page != $cntNum) {
            echo " <a href='" . $link . "'>尾页</a>";
        }
        echo $this->jump_page();
        echo "</div>";
    }

}